//
//  NotificationListVC.m
//  students
//
//  Created by 任辰东 on 2024/2/23.
//

#import "NotificationListVC.h"
#import "NotificationTCell.h"
#import "RCDEmptyView.h"
#import "NotificationDetailVC.h"
#import "NotificationApis.h"

@interface NotificationListVC ()

@end

@implementation NotificationListVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"通知";
    
//    [self setNavRightItemWithTitle:@"全部已读" titleColor:UIColorFromHex(0x000000)];
    
    [self setupEstimatedRowHeight:200 cellClasses:@[[NotificationTCell class]]];
    [self beginRefresh];
    
    self.edgesForExtendedLayout = UIRectEdgeNone;
    
    self.myTableView.mj_footer = nil;
    
//    WeakSelf
//    [RACObserve([GVUserDefaults standardUserDefaults], isLoginSuccess) subscribeNext:^(NSNumber *  _Nullable x) {
//        StrongSelf
//
//        if (x.integerValue == 0) {
////            [self.dataSourceArr removeAllObjects];
////            [self.myTableView reloadData];
//            [self setNavRightItemWithImage:nil highlightImg:nil];
////            [self addEmptyView];
//        }else{
//            
//            [self setNavRightItemWithImage:UIImageWithName(@"notification_all") highlightImg:UIImageWithName(@"notification_all")];
//        }
//        [self beginRefresh];
//    }];
}

-(void)RightBarItemClick:(UIButton *)sender
{
    DLog(@"全部已读");
//    NSMutableDictionary *params = [NSMutableDictionary dictionary];
//    params[@"user_id"] = [GVUserDefaults standardUserDefaults].uid;
//
//    WeakSelf
//    [self showLoadingHUDWithMessage:nil];
//    [NotificationApis upMessageAllRequestWithParams:params SuccessBlock:^(NSNumber *code, NSString *msg, id responseData) {
//        StrongSelf
//        [self hideLoadingHUD];
//        [self beginRefresh];
//        //消息处理
//        [[PushManager sharedInstance] setupPushNumber];
//
//    } FailureBlock:^(NSNumber *code, NSString *msg) {
//        StrongSelf
//        [self showWarningHUDWithMessage:msg completion:NULL];
//    }];
}

-(void)requestDataWithOffset:(NSInteger)offset success:(void (^)(NSArray *))success failure:(void (^)(NSString *))failure
{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"id_card"] = [GVUserDefaults standardUserDefaults].id_card;
//    params[@"id_card"] = @"P410882201608280107";
//    params[@"page"] = @(offset);
//    params[@"pageSize"] = @(10);

    WeakSelf
    [self showLoadingHUDWithMessage:nil];
    [NotificationApis getMessageListRequestWithParams:params SuccessBlock:^(NSNumber *code, NSString *msg, id responseData) {
        StrongSelf
        [self hideLoadingHUD];
        NSArray *data = [NotificationModel mj_objectArrayWithKeyValuesArray:responseData];
        success(data);
        [self addEmptyView];
    } FailureBlock:^(NSNumber *code, NSString *msg) {
        StrongSelf
        [self showWarningHUDWithMessage:msg completion:NULL];
    }];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    NotificationModel *model;
    if (indexPath.row < self.dataSourceArr.count) {
        model = self.dataSourceArr[indexPath.row];
    }

    NotificationDetailVC *detailVC = [[NotificationDetailVC alloc]initWithModel:model];
    [self.navigationController pushViewController:detailVC animated:YES];
    
    WeakSelf
    [detailVC setUpdateNotificationBlock:^{
            StrongSelf
        model.status = @"1";
        [self.dataSourceArr replaceObjectAtIndex:indexPath.row withObject:model];
        [self.myTableView reloadData];
    }];
}


-(void)addEmptyView
{
    if (IsEmpty(self.dataSourceArr)) {
        [RCDEmptyView addEmptyWithStyle:EmptyViewStyleCommon onView:self.myTableView position:EmptyViewPositionCenter clickCallBack:nil];
    }else{
        [RCDEmptyView removeFromParentView:self.myTableView];
    }
}

-(void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}


@end
